/**
 * Author : Athirai
 */
package sg.edu.nus.iss.vmcs.store;
import sg.edu.nus.iss.vmcs.exceptions.InvalidCoinException;

public abstract class CoinHandler {
	private CoinHandler successor;
	private Coin coin;

	public CoinHandler(CoinHandler successorHandler, Coin coin) {
		this.coin = coin;
		this.successor = successorHandler;
	}

	protected Coin getCoin() {
		return coin;
	}

	public Coin handle(double weight) throws InvalidCoinException {
		if (successor != null) {
			return successor.handle(weight);
		} else {
			System.out
			.println("\t\t\t\tI donot have a successor ");
			throw new InvalidCoinException("Invaild Coin");
		}

	}

}

class ConcreteCoinHandler extends CoinHandler {

	public ConcreteCoinHandler(CoinHandler successorHandler, Coin coin) {
		super(successorHandler, coin);
	}

	public Coin handle(double weight) throws InvalidCoinException {

		Coin c = getCoin();
		System.out.print(" I am " + c.getValue() + "cent CoinHandler------->");
		if (c.getWeight() == weight) {

			System.out.println(" I handled the coin my value is "
					+ c.getValue() + " cent");
			return c;
		} else {

			System.out
					.println("  I cannot handle I am passing to my succesor ");
			return super.handle(weight);

		}

	}

}
